import React from 'react'
import ViewRouteDto from '@/ui/antd/service/router/types/model/dto/ViewRouteDto'

import { isNil } from 'lodash-es'
import useCurrentRoutePathStore from '@/ui/antd/store/useCurrentRoutePathStore'
import Home from '@/ui/antd/pages/main/view/home/Home'
import applicationRoutes from '@/ui/antd/service/router/routes/applicationRoutes'
import demoRoutes from '@/ui/antd/service/router/routes/demoRoutes'
import oauth2Routes from '@/ui/antd/service/router/routes/oauth2Routes'
import captchaRoutes from '@/ui/antd/service/router/routes/captchaRoutes'
import upmsRoutes from '@/ui/antd/service/router/routes/upmsRoutes'
import accountRoutes from '@/ui/antd/service/router/routes/accountRoutes'
import ossRoutes from '@/ui/antd/service/router/routes/ossRoutes'
import smsRoutes from '@/ui/antd/service/router/routes/smsRoutes'
import captchaSmsRoutes from '@/ui/antd/service/router/routes/captchaSmsRoutes'

const defaultRoute: ViewRouteDto = {
	path: '/home',
	defaultFlag: true,
	title: '首页',
	element: <Home />,
}

export const allRoutes: ViewRouteDto[] = [
	defaultRoute,
	...applicationRoutes,
	...demoRoutes,
	...oauth2Routes,
	...captchaRoutes,
	...upmsRoutes,
	...accountRoutes,
	...smsRoutes,
	...captchaSmsRoutes,
	...ossRoutes,
]

class ViewRouterService {
	getDefaultRoute = (): ViewRouteDto => {
		return defaultRoute
	}
	getRouteByPath = (path: string): ViewRouteDto | undefined => {
		const route = allRoutes.find(value => value.path === path)
		return route
	}
	getRouteOrDefaultByPath = (path: string): ViewRouteDto | undefined => {
		const route = this.getRouteByPath(path)
		if (!route) {
			return this.getDefaultRoute()
		}
		return route
	}

	getRoutesByPaths = (paths: string[]): ViewRouteDto[] => {
		const routes = allRoutes.filter(value => paths.includes(value.path))
		return routes
	}

	isDefaultRoute = (item: ViewRouteDto): boolean => {
		if (isNil(item)) {
			return false
		}
		return item.path === this.getDefaultRoute().path
	}

	getCurrentOrDefaultRoute = (): ViewRouteDto => {
		const currentRoute = this.getCurrentRoute()
		if (isNil(currentRoute)) {
			return this.getDefaultRoute()
		}
		return currentRoute
	}

	getCurrentRoute = (): ViewRouteDto | undefined => {
		const currentPath = useCurrentRoutePathStore.getState().path
		if (isNil(currentPath)) {
			return undefined
		}
		return this.getRouteByPath(currentPath)
	}

	setCurrentRouteByPath = (path: string): void => {
		useCurrentRoutePathStore.getState().setPath(path)
	}

	setCurrentRoute = (route: ViewRouteDto | undefined): void => {
		useCurrentRoutePathStore.getState().setPath(route?.path)
	}
}

export default new ViewRouterService()
